package com.biz.primus.ms.auth.dao.po;

import com.biz.primus.base.enums.CommonStatus;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import com.biz.primus.model.sso.auth.vo.IDescribableItem;
import com.biz.primus.ms.base.jpa.po.BaseEntity;
import org.hibernate.validator.constraints.NotBlank;

@Entity
@Table(name = "auth_resource", uniqueConstraints = {@UniqueConstraint(columnNames = {"applicationType", "customId"})})
public class Resource extends BaseEntity implements ICustomIdAble, IAuthoritiesAble, IDescribableItem {

    private static final long serialVersionUID = 5242502895214313530L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(length = 64)
    private String applicationType;

    @Column(length = 64, nullable = false)
    private String customId;

    @Column(length = 50, nullable = false)
    @NotNull(message = "名字不能为空")
    @NotBlank(message = "名字不能为空")
    private String name;

    @Column(columnDefinition = "MEDIUMTEXT")
    //TODO 让味精检查
//    @NotBlank(message = "权限不能为空")
//    @NotNull(message = "权限不能为空")
//    @Pattern(regexp = "((ROLE)|(OPT))(_[A-Z]+)+(;((ROLE)|(OPT))(_[A-Z]+)+)*;?", message = "无效的权限")
    private String authorities;

    @Column
    private String description;

    private Integer orderIndex;

    @ManyToOne(fetch = FetchType.LAZY)
    private MenuItem menuItem;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "auth_role_resource",
            joinColumns = {@JoinColumn(name = "resource_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")},
            uniqueConstraints = {@UniqueConstraint(columnNames = {"resource_id", "role_id"})})
    private List<Role> roles;

    @Column
    @Enumerated(EnumType.STRING)
    private CommonStatus status = CommonStatus.ENABLE;

    public Resource() {

    }

    public Resource(Long id, String name, String authorities, String description, MenuItem menuItem, List<Role> roles) {
        setId(id);
        this.name = name;
        this.authorities = authorities;
        this.description = description;
        this.menuItem = menuItem;
        this.roles = roles;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    /**
     * {@linkplain Resource#applicationType}
     */
    public String getApplicationType() {
        return applicationType;
    }

    /**
     * {@linkplain Resource#applicationType}
     */
    public void setApplicationType(String applicationType) {
        this.applicationType = applicationType;
    }

    /**
     * {@linkplain Resource#customId}
     */
    public String getCustomId() {
        return customId;
    }

    /**
     * {@linkplain Resource#customId}
     */
    public void setCustomId(String customId) {
        this.customId = customId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthorities() {
        return authorities;
    }

    public void setAuthorities(String authorities) {
        this.authorities = authorities;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * {@linkplain Resource#orderIndex}
     */
    public Integer getOrderIndex() {
        return orderIndex;
    }

    /**
     * {@linkplain Resource#orderIndex}
     */
    public void setOrderIndex(Integer orderIndex) {
        this.orderIndex = orderIndex;
    }

    public MenuItem getMenuItem() {
        return menuItem;
    }

    public void setMenuItem(MenuItem menuItem) {
        this.menuItem = menuItem;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public CommonStatus getStatus() {

        return status;
    }

    public void setStatus(CommonStatus status) {

        this.status = status;
    }

    public void copy(Resource res) {
        this.name = res.name;
        this.description = res.description;
        this.authorities = res.authorities;
    }

}
